import axios from "axios";
import Qs from 'qs';
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from "axios";
import { ElMessage } from 'element-plus';
import { useRouter, useRoute } from "vue-router";

const baseURL = `http://localhost:48080`
// const baseURL = `http://127.0.0.1:48080`
import router from "@/router";
// const router = useRouter();
const route = useRoute();

type Result<T> = {
    status: number;
    message: string;
    data: any
};

// 导出Request类，可以用来自定义传递配置来创建实例
export class Request {
    // axios 实例
    instance: AxiosInstance;
    // 基础配置，url和超时时间
    baseConfig: AxiosRequestConfig = { baseURL, timeout: 60000 };

    constructor(config: AxiosRequestConfig) {
        // 使用axios.create创建axios实例
        this.instance = axios.create(Object.assign(this.baseConfig, config));

        this.instance.interceptors.request.use(
            (config: InternalAxiosRequestConfig) => {
                console.log(config, "config")
                // 一般会请求拦截里面加token，用于后端的验证
                const token = sessionStorage.getItem("accessToken") as string
                if (token) {
                    config.headers!.Authorization = `Bearer ${token}`;
                }

                // if (config?.headers['Content-Type'] === "multipart/form-data") {
                //     config.headers['Content-Type'] = "multipart/form-data"
                // }
                return config;
            },
            (err: any) => {
                // 请求错误，这里可以用全局提示框进行提示
                return Promise.reject(err);
            }
        );

        this.instance.interceptors.response.use(
            (res: AxiosResponse) => {
                console.log(res, "res")
                if ('size' in res.data) {
                    return Promise.resolve(res.data)
                }
                // 直接返回res，当然你也可以只返回res.data
                // 系统如果有自定义code也可以在这里处理
                if (res.data.code === 0) {
                    return Promise.resolve(res.data);
                } else {

                    let pathRouterPath = '';
                    // if (window.sessionStorage.getItem('userRole') === '/fillList') {
                    //     pathRouterPath = '/login?type=fillList'
                    // } else {
                    //     pathRouterPath = '/login?type=review'
                    // }
                    pathRouterPath = '/login?type=fillList'

                    if (res.data.code === 401) {
                        router.replace(pathRouterPath)
                    }
                    let msg = res.data.message || res.data.msg
                    ElMessage.error(msg)
                    return Promise.reject(msg);
                }

            },
            (err: any) => {
                // 这里用来处理http常见错误，进行全局提示
                let message = "";
                switch (err.response.status) {
                    case 400:
                        message = "请求错误(400)";
                        break;
                    case 401:
                        message = "未授权，请重新登录(401)";
                        // 这里可以做清空storage并跳转到登录页的操作
                        break;
                    case 403:
                        message = "拒绝访问(403)";
                        break;
                    case 404:
                        message = "请求出错(404)";
                        break;
                    case 408:
                        message = "请求超时(408)";
                        break;
                    case 500:
                        message = "服务器错误(500)";
                        break;
                    case 501:
                        message = "服务未实现(501)";
                        break;
                    case 502:
                        message = "网络错误(502)";
                        break;
                    case 503:
                        message = "服务不可用(503)";
                        break;
                    case 504:
                        message = "网络超时(504)";
                        break;
                    case 505:
                        message = "HTTP版本不受支持(505)";
                        break;
                    default:
                        message = `连接出错(${err.response.status})!`;
                }
                return Promise.reject('Network error');
            }
        );
    }

    // 定义请求方法
    public request(config: AxiosRequestConfig): Promise<AxiosResponse> {
        return this.instance.request(config);
    }

    public get<T = any>(
        url: string,
        config?: any,
    ): Promise<any> {
        return this.instance.get(url, { params: config });
    }

    public downFile<T = any>(
        url: string,
        config?: any,
    ): Promise<any> {
        return this.instance.get(url, { responseType: 'blob' });
    }

    public post<T = any>(
        url: string,
        data?: any,
        config?: AxiosRequestConfig
    ): Promise<any> {
        if (config?.headers['Content-Type'] === "multipart/form-data") {
            return this.instance.post(url, data, config);
        }
        return this.instance.post(url, data, config);

    }

    public put<T = any>(
        url: string,
        data?: any,
        config?: AxiosRequestConfig
    ): Promise<any> {

        return this.instance.put(url, data, config);
    }

    public delete<T = any>(
        url: string,
        config?: AxiosRequestConfig
    ): Promise<any> {
        return this.instance.delete(url, config);
    }
}

// 默认导出Request实例
export default new Request({})
